<template>
  <div>
    <van-nav-bar left-arrow @click-left="leftClick"></van-nav-bar>
    <div class="login-form">
      <h3 class="title">短信登录</h3>
      <van-form ref="form">
        <van-field
          name="mobile"
          v-model="form.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        ></van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #button>
            <div class="code" @click="sendCode" v-if="totalTime === 7">
              发送验证码
            </div>
            <div class="code" v-else>{{ totalTime }}秒后重新发送</div>
          </template>
        </van-field>
        <van-button class="btn" @click="submit">登录</van-button>
      </van-form>
    </div>
  </div>
</template>

<script>
import { authorizations, smsCodes } from '@/api/user.js'
export default {
  data () {
    return {
      totalTime: 7,
      form: {
        mobile: '13200000000',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return reg.test(value)
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              return value.length === 6
            },
            message: '请输入6位验证码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  methods: {
    leftClick () {},
    // 登录
    submit () {
      this.$refs.form.validate().then(async () => {
        // this.$toast.success('验证成功')
        this.$toast.loading({ duration: 0 })
        const res = await authorizations(this.form)
        // 储存token到vuex
        this.$store.commit('user/setToken', res.data.token)
        // 有值就跳转到响应的路由,没有就跳转到首页
        const redirect = this.route.query.redirect || '/'
        this.$router.push(redirect)
        this.$toast.success('登录成功')
        console.log('res:', res)
      })
      // .catch(() => {
      //   this.$toast.fail('验证失败')
      // })
    },
    // 发送验证码
    sendCode () {
      this.$refs.form.validate('mobile').then(async () => {
        this.totalTime--
        this.interval = setInterval(() => {
          console.log(this.totalTime)
          this.totalTime--
          if (this.totalTime <= 0) {
            // 清除倒计时
            clearInterval(this.interval)
            // 还原时间
            this.totalTime = 7
          }
        }, 1000)
        await smsCodes(this.form.mobile)
        this.$toast.success('发送成功')
      })
    }
  },
  // 销毁倒计时
  beforeDestroy () {
    clearInterval(this.interval)
  },
  // 中断渲染
  destroyed () {}
}
</script>

<style lang="less" scoped>
::v-deep .van-nav-bar .van-icon {
  color: #999;
}
.login-form {
  padding: 20px;
}
.title {
  font-size: 18px;
  font-weight: 400;
  padding-left: 15px;
}
.code {
  font-size: 14px;
  color: #ccc;
}
.btn {
  width: 100%;
  background-color: #fd514a;
}
</style>
